/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Net.Sockets;

namespace DC
{
    public class DCStream : Stream
    {
        private readonly long _length;
        private readonly NetworkStream _netStream;
        private readonly byte[] _fileData;
        private int _bytesCopied = 0;

        public DCStream(NetworkStream netStream, long length, byte[] fileData)
        {
            _netStream = netStream;
            _length = length;
            _fileData = fileData;
        }

        public override int Read(byte[] buffer, int offset, int size)
        {
            int countBytesToCopyFromArray = Math.Min(size, _fileData.Length - _bytesCopied);
            int bytesProcessed = 0;
            
            if (countBytesToCopyFromArray < 0)
                countBytesToCopyFromArray = 0;

            if (countBytesToCopyFromArray > 0)
            {
                Array.Copy(_fileData, _bytesCopied, buffer, 0, countBytesToCopyFromArray);
                _bytesCopied += countBytesToCopyFromArray;
                bytesProcessed += countBytesToCopyFromArray;
            }

            if (countBytesToCopyFromArray < size)
            {
                bytesProcessed += _netStream.Read(buffer, countBytesToCopyFromArray, size - countBytesToCopyFromArray);
            }
            return bytesProcessed;
        }

        public override long Length
        {
            get { return _length; }
        }

        public override void Close()
        {
            Dispose(true);
        }

        protected override void Dispose(bool disposing)
        {
            _netStream.Dispose();
            base.Dispose(disposing);
        }

        #region "Stream class stubs"
        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override bool CanSeek
        {
            get { return false; }
        }

        public override long Position
        {
            get { throw new NotSupportedException(); }
            set { throw new Exception("The method or operation is not implemented."); }
        }

        public override void Flush()
        {
            throw new NotSupportedException();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
        #endregion
    }
}
